<template>
  <div class="department-container">
    <div class="app-container">
      <el-card shadow="always" :body-style="{ padding: '20px' }">
        <!-- card title -->
        <template #header>
          <!-- 用一个行列布局 -->
          <el-row>
            <el-col :span="20">
              <span>{{ company.name }}</span>
            </el-col>
            <el-col :span="4">
              <el-row type="flex" justify="end">
                <!-- 两个内容 -->
                <el-col>负责人</el-col>
                <!-- 下拉菜单 element -->
                <el-col>
                  <el-dropdown>
                    <span> 操作<i class="el-icon-arrow-down" /> </span>
                    <el-dropdown-menu slot="dropdown">
                      <el-dropdown-item @click.native="addChildDept()">
                        添加子部门
                      </el-dropdown-item>
                    </el-dropdown-menu>
                  </el-dropdown>
                </el-col>
              </el-row>
            </el-col>
          </el-row>
        </template>

        <!-- card body -->
        <el-tree
          :data="departList"
          :props="defaultProps"
          default-expand-all
          @node-click="handleNodeClick"
        >
          <!-- 默认插槽和作用域插槽 -->
          <template #default="{ data }">
            <el-row style="width: 100%">
              <el-col :span="20">
                <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager || "--" }}</el-col>
                  <el-col>
                    <!-- 下拉菜单 element -->
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <!-- 下拉菜单 -->
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="addChildDept(data, 1)">
                          添加子部门
                        </el-dropdown-item>
                        <el-dropdown-item @click.native="addChildDept(data, 2)">
                          编辑部门
                        </el-dropdown-item>
                        <el-dropdown-item @click.native="delCurr(data)">
                          删除部门
                        </el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
    </div>
    <!--  新增部门弹出层 -->
    <AddEdit
      ref="editDept"
      :show="show"
      :curr-dept="currDept"
      :all-depts="allDepts"
      @update-list="getDepartsList"
      @close-dialog="show = $event"
    />
  </div>
</template>

<script>
import { getDepartments, delDepartments } from '@/api/departments'
import { transformTreeData } from '@/utils'
import AddEdit from './components/add-dept.vue'
export default {
  components: {
    AddEdit
  },
  data () {
    return {
      // 添加子部门的当前数据
      currDept: null,
      // 控制弹层
      show: false,
      company: { name: '', manange: 'CEO' },
      // 平面组织数据, 方便校验编码的唯一需要遍历
      allDepts: [],
      departList: [
        // {
        //   name: '总裁办'
        // },
        // {
        //   name: '财务部',
        //   children: [{
        //     name: '财务核算部'
        //   }]
        // }
      ],
      defaultProps: {
        children: 'children',
        label: 'name'
      }
    }
  },
  created () {
    this.getDepartsList()
  },
  methods: {
    // 添加弹层
    addChildDept (currDept, type) {
      this.show = true
      this.currDept = currDept
      // 编辑
      if (type === 2) {
        this.$refs.editDept.getDetail(currDept.id)
      }
    },
    // 删除数据
    async delCurr (currData) {
      // console.log(currData)
      try {
        // 返回promise对象
        await this.$confirm(`确认删除${currData.name}吗?`, '提示')
        if (currData.children && currData.children.length > 0) {
          return this.$message.error('请先删除子部门')
        }
        await delDepartments(currData.id)
        // 刷新列表
        this.getDepartsList()
        this.$message.success('删除成功')
      } catch (e) {
        console.log(1)
      }
    },
    // 获取组织数据
    async getDepartsList () {
      const { companyName, depts } = await getDepartments()
      // console.table(depts)
      // 给子所有平面数据
      this.allDepts = depts
      this.company.name = companyName
      // 处理数据
      // transformTreeData(depts)
      // console.log(transformTreeData(depts))
      this.departList = transformTreeData(depts)
    },
    // 点击文字才会触发
    handleNodeClick (data) {
      // console.log(data)
    }
  }
}
</script>

<style lang="sass" scoped>
::v-deep .el-tree-node__content
  margin: 3px 0
  font-size: 14px
</style>
